package com.briup.jz.service.impl;

import com.briup.jz.bean.OrderLine;
import com.briup.jz.bean.OrderLineExample;
import com.briup.jz.dao.OrderLineMapper;
import com.briup.jz.service.IOrderLineService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OrderLineImpl implements IOrderLineService {
    @Autowired
    private OrderLineMapper orderLineMapper;
    @Override
    public void saveOrUpdate(OrderLine orderLine) throws CustomerException {
//        OrderLineExample example=new OrderLineExample();
        if (orderLine.getId()!=null){
//            判断用户是否输入主键信息
            orderLineMapper.insert(orderLine);
        }else {
//            判断有id 的订单项是否为空
            OrderLine orderLine1 = orderLineMapper.selectByPrimaryKey(orderLine.getId());

            if (orderLine1==null){
                //            如果为空则为插入操作
                orderLineMapper.insert(orderLine);
            }
            orderLineMapper.updateByPrimaryKey(orderLine);

        }
    }

    @Override
    public void deleteById(Long id) throws CustomerException {
//        根据id 查找该订单项
        OrderLine orderLine =orderLineMapper.selectByPrimaryKey(id);
//        如找不到则订单项不存在
        if (orderLine==null){
            throw  new CustomerException("该订单项不存在");
        }
        orderLineMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<OrderLine> query(Long id, Long proId, Long orderId) {
        OrderLineExample example =new OrderLineExample();
        OrderLineExample.Criteria criteria=example.createCriteria();
        if (id!=null){
//            如果主键不为空则按照主键查询
           criteria.andIdEqualTo(id);
        }
        if (proId!=null){
//            判断产品id是否为空,如果不为空则按照产品id 查询
          criteria.andProductIdEqualTo(proId);
        }
        if (orderId!=null){
//            判断订单id是否为空,如果不为空则按照订单号查询
         criteria.andOrderIdEqualTo(orderId);
        }
//        按照条件查寻
        return orderLineMapper.selectByExample(example);
    }


}
